package com.cskaoyan.service.impl;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketBrand;
import com.cskaoyan.bean.pojo.MarketBrandExample;
import com.cskaoyan.bean.vo.BrandListVO;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022/04/06 21:29
 *
 * @author HanWeiqi
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public BrandListVO brandList(BaseParam baseParam, Integer id, String name) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        if(name!=null && !"".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);
        marketBrandExample.setOrderByClause(baseParam.getSort()+" "+baseParam.getOrder());
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return BrandListVO.data(total, pages, baseParam.getPage(), baseParam.getLimit(), marketBrands);
    }

    @Override
    public int brandDelete(MarketBrand marketBrand) {
        marketBrand.setDeleted(true);
        return marketBrandMapper.updateByPrimaryKey(marketBrand);
    }

    @Override
    public int brandCreate(MarketBrand marketBrand) {
        marketBrand.setAddTime(new Date());
        marketBrand.setUpdateTime(new Date());
        return marketBrandMapper.insertSelective(marketBrand);
    }

    @Override
    public int brandUpdate(MarketBrand marketBrand) {
        marketBrand.setUpdateTime(new Date());
        return marketBrandMapper.updateByPrimaryKey(marketBrand);
    }
}
